runOnStartup(async runtime => {

    lib.fpsController.p = "fpp"
    lib.fpsController.enble = false
    lib.fpsController.fly = false

    runtime.objects.Pl.addEventListener("instancecreate", (e) => {
        lib.fpsController["fpscam:init:set"]()
        runtime.addEventListener("tick", () => {
            lib.fpsController["fpscam:tick:set"]()
            lib.fpsController["pl_movement_collsion"]()

        })
    })
})

lib.fpsController = {
    "fpscam:init:set": () => {
        /**
         * @type {I3DShapeInstance}
         * @private
         */
        var _pl = runtime.objects.Pl?.getFirstInstance()
        /**
         * @type {I3DCameraObjectType}
         */
        if (lib.addon_cam.debugcam) return
        var _fpscam = runtime.objects.fpscam
        _fpscam.lookParallelToLayout(0, 0, 0, 0)

        lib.addEventListener("_z_press", () => {
            //lib2.callFunction("mouse:request:pointer:lock")
            if (!lib.fpsController.enble) {
                setTimeout(function () {
                    lib.fpsController.enble = true
                    lib.c.call("mouse:request:pointer:lock")

                }, 10)
            }
            if (lib.fpsController.enble) {
                lib.fpsController.enble = false
                lib.c.call("mouse:realase:pointer:lock")
            }
        })

        lib.addEventListener("_mouse:onmovement", () => {
            //lib2.callFunction("cam:rotate:bymousem")
            var MosueSpd = 15
            if (!lib.fpsController.enble) return
            var mouse_move_ment_x = lib.c.call("get_mouse_move_x") / MosueSpd
            var mouse_move_ment_y = lib.c.call("get_mouse_move_y") / MosueSpd
            lib.c.call("cam:rotate:bymousem", mouse_move_ment_x, mouse_move_ment_y, 20, 180)


        })


        lib.addEventListener("_q_press", () => {
            if (lib.fpsController.fly && _pl.zElevation > 0) {
                _pl.zElevation = 5
            }
        })
    },

    "fpscam:tick:set": () => {
        if (lib.addon_cam.debugcam) return
        var _pl = runtime.objects.Pl?.getFirstInstance()
        var _fpscam = runtime.objects.fpscam
        //_pl.angle = lib2.callFunction("cam_get_x_rotation")
        lib.c.call("cam:set:postion", _pl.x, _pl.y, _pl.zElevation + 25)

    },

    "pl_movement_collsion": () => {
        var x_rotation = lib.c.call("cam_get_x_rotation")
        // Walk
        var spd = 500 * runtime.dt
        var _pl = runtime.objects.Pl?.getFirstInstance()

        if (runtime.keyboard.isKeyDown("KeyW")) {

            lib2.callFunction("pl:move:at:angel", x_rotation, spd)
            lib2.callFunction("pl:out:of:soild", x_rotation + 180)
        }
        if (runtime.keyboard.isKeyDown("KeyS")) {
            lib2.callFunction("pl:move:at:angel", x_rotation, -spd)
            lib2.callFunction("pl:out:of:soild", x_rotation)
        }
        if (runtime.keyboard.isKeyDown("KeyA")) {
            lib2.callFunction("pl:move:at:angel", x_rotation - 90, spd)
            lib2.callFunction("pl:out:of:soild", x_rotation + 90)
        }
        if (runtime.keyboard.isKeyDown("KeyD")) {
            lib2.callFunction("pl:move:at:angel", x_rotation + 90, spd)
            lib2.callFunction("pl:out:of:soild", x_rotation - 90)
        }
        //Fly
        if (!lib.fpsController.fly) return
        if (runtime.keyboard.isKeyDown("Space")) {
            _pl.zElevation += 3
        }
        if (runtime.keyboard.isKeyDown("ShiftLeft")) {
            _pl.zElevation -= 3
        }

    }
}